package com.client.dbdao;

import com.client.bean.UserInfo;
import com.data.bean.Account;
import com.data.dbdao.BaseDao;
import com.data.util.LoginMethodUtils;
import com.data.util.Md5CodeUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;

/**
 * 用户管理
 *
 * @author:HingLo
 * @create 2017-10-12 9:16
 **/

@Repository
@Transactional
public class UserInfoClientDao extends BaseDao {
    private static Logger logger = Logger.getLogger(UserInfoClientDao.class);

    /**
     * 登录并返回用户信息
     *
     * @param accountType
     * @param username
     * @param password
     * @return
     */
    public List<?> userLoginDao(String accountType, String username, String password) {
        StringBuilder hql = new StringBuilder("select a.id,u.username,a.email,u.sex,u.token ,u.ctime from Account as a,UserInfo as u where a.id=u.uid and a.password=? and a.");
        String sql = LoginMethodUtils.getHql(hql, accountType);
        List<?> list = super.findMassageDao(sql, password, username);
        return list;
    }

    /**
     * 头像查看
     *
     * @param id
     * @return
     */
    public List<UserInfo> lookHeaderDao(String id) {
        String hql = "from UserInfo where uid=?";
        return (List<UserInfo>) super.findMassageDao(hql);
    }

    /**
     * 更换头像,性别，邮箱，用户名,token
     *
     * @param uid
     * @param param
     * @return
     */
    public boolean updateHeaderDao(int type, String uid, Object param) {
        String hql;
        switch (type) {
            case 0:
                hql = "update UserInfo set header=? where uid=?";
                break;
            case 1:
                hql = "update UserInfo set sex=? where uid=?";
                break;
            case 3:
                hql = "update Account set email=? where id=?";
                break;
            case 4:
                hql = "update UserInfo set username=? where id=?";
                break;
            case 5:
                hql = "update UserInfo set token=? where uid=?";
                break;
            default:
                return false;
        }
        if (type == 3) {
            if (super.updateOneMssageDao(hql, param, uid)) {
                hql = "update UserInfo set token=? where uid=?";
                return super.updateOneMssageDao(hql, null, uid);
            }
        }
        return super.updateOneMssageDao(hql, param, uid);
    }

    /**
     * 更新密码
     *
     * @param id
     * @return
     */
    public boolean updatePasswordDao(String id, String password) {
        String sql = "update Account as a,UserInfo as u set a.password=?,u.token=null where a.id=u.uid and  a.id=? ";
        if (super.updateMysqlMethodDao(sql, password, id) > 0) {
            String token = Md5CodeUtils.md5(id + new Date().toString());
            //更新token
            return updateHeaderDao(5, id, token);
        }
        return false;
    }

    /**
     * 令牌校验
     *
     * @param id
     * @param token
     * @return
     */
    public List<?> chenkUserInfoTokenDao(String id, String token) {
        String hql = "from UserInfo where id=? and token=?";
        return super.findMassageDao(hql, id, token);
    }

    public List<UserInfo> lookUserInfoHeaderDao(String uid) {
        String hql = "from UserInfo where uid=?";
        return (List<UserInfo>) super.findMassageDao(hql, uid);
    }

    /**
     * 检测账号是否已经存在
     *
     * @param res
     * @param username
     * @return true表示可以用，false不可以用
     */
    public boolean checkEmailDao(String res, String username) {
        StringBuilder tempHql = new StringBuilder("from Account where ");
        String hql = LoginMethodUtils.getHql(tempHql, res);
        return super.findMassageDao(hql, username).size() <= 0;
    }

    /**
     * 检测邮箱是否在数据库中
     *
     * @param email
     * @return
     */
    public List<?> forgetPasswordCheckEmailDao(String email) {
        String hql = "from Account where email=?";
        return super.findMassageDao(hql, email);
    }

    /**
     * 重置密码
     *
     * @param email
     * @param password
     * @return
     */
    public boolean settingPasswordDao(String email, String password) {
        String hql = "update Account set password=? where email=?";
        if (super.updateOneMssageDao(hql, password, email)) {
            List<Account> list = (List<Account>) forgetPasswordCheckEmailDao(email);
            if (list != null && list.size() == 1) {
                Account account = list.get(0);
                String id = account.getId();
                String token = Md5CodeUtils.md5(id + new Date().toString());
                //更新token
                return updateHeaderDao(5, id, token);

            }

        }
        return false;
    }

    /**
     * 通过id与token来查询账号信息
     *
     * @param id    账号的唯一id
     * @param token 唯一token
     * @return 返回查询结果集合
     */
    public List<?> lookUserInfoTokenDao(String id, String token) {
        String hql = "select a.id,u.username,a.email,u.sex,u.token, u.ctime from Account as a,UserInfo as u where a.id=u.uid and a.id=? and u.token=?";
        return super.findMassageDao(hql, id, token);
    }

    /**
     * 退出登录，直接设置token为null就可以了
     *
     * @param uid
     * @return
     */
    public boolean updateloginOutDao(String uid) {

        String hql = "update UserInfo set token=? where uid=?";
        return super.updateOneMssageDao(hql, null, uid);
    }
}
